package myInteger;

public class MyInteger {

    // 定义一个静态的内部类，用于存储享元对象
    private static class MyIntegerHolder {
        private static final MyInteger[] pool = new MyInteger[1500];

        static {
            for (int i = -321; i <= 321; i++) {
                pool[i + 321] = new MyInteger(i);
            }
        }

        public static MyInteger getInstance(int num) {
            if (num < -321 || num > 321) {
                return new MyInteger(num);
            }
            return pool[num + 321];
        }
    }

    private final int value;

    // 私有构造函数，防止外部直接创建对象
    private MyInteger(int value) {
        this.value = value;
    }

    // 公共静态方法，用于获取享元对象
    public static MyInteger getInstance(int num) {
        return MyIntegerHolder.getInstance(num);
    }

}